<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class User extends MY_Controller {
		
	function __construct(){
		parent::__construct();
		
		/** 登陆验证 */
		if(!isset($_SESSION)){
		    session_start();
		}
		$admin = $this->session->userdata('admin_user');
		if(!$admin || !isset($admin['user_id'])){
			header('Location: /sys_admin/login');
			exit;
		}
		
		$this->tab ='user';
		
	}
			// 20150430 身份证认证
	function verify_realuser(){
		$user_id = intval($this->input->post('user_id'));
		$status = intval($this->input->post('status'));
		$setArr = array('real_status' => $status);	
		$this->load->model('User_model', 'user');
		$flag = $this->user->update($setArr,array('user_id'=>$user_id));
		echo $flag;
		
	}
	
	/**
	 * 用户列表
	 */
	function zhs_ulist(){
		$per_page  = intval($this->input->get('per_page'));
		$page_size = 10;
		$parms     = array(
			'username'   => trim($this->input->get('username')),
			'realname'   => trim($this->input->get('realname')),
			'email'      => trim($this->input->get('email')),
			'type_id'	 => intval($this->input->get('type_id')),
			'add_time'	 => $this->input->get('add_time'),
			'phone'   		=> trim($this->input->get('phone')),
		);
		
		$data = array();
		
		$this->load->model('User_model', 'user');
		
		//$islock = 0;
		$data = $this->user->get_user_list($parms,$per_page );
		$data['parms'] = $parms;
		
		//分页
		$this->load->library('pagination');
		$config['base_url'] = '/sys_admin/user/zhs_ulist?username='.$parms['username'].'&realname='.$parms['realname'].'&email='.$parms['email'].'&type_id='.$parms['type_id'].'&add_time='.$parms['add_time'];
		$config['total_rows'] = $data['user_count'];
		$config['per_page'] = $page_size; 
		$this->pagination->initialize($config); 
		$data['title'] = '用户列表';
		
		
		$data['userFstatus'] = $this->user->arrays('userFstatus');
		$data['tab'] = $this->tab;
		$data['sub'] = 'ulist';
		
		$this->load->view('/sys_admin/user/user_list', $data);
	}
	
	/**
	 * 发送站内信，邮件，短信
	 */
	function send(){
		
		$user_id = $_POST['uid'];
		$user_id_str = $_POST['uidstr'];
		
		$this->load->model('User_model', 'user');
		if($user_id > 0){
			$minfo = $this->user->get_by_uid($user_id);
			$vdata['user_id'] = $minfo['user_id'];	
		}else{
			$vdata['user_id'] = $user_id_str;
		}	
		
		
		$this->load->model('Systemtemplet_model');
		
		//1:邮件模板；2：短信模板；3：站内信模板
		$vdata['emailtemplets'] = $this->Systemtemplet_model->get_templet_list(1);
		$vdata['smstemplets'] = $this->Systemtemplet_model->get_templet_list(2);
		$vdata['msgtemplets'] = $this->Systemtemplet_model->get_templet_list(3);
		
		$response_html = $this->load->view('/sys_admin/user/user_send',$vdata,true);
		
		echo $response_html;
	}
	
	function change_send_content(){
		$id = $_POST['id'];
		$this->load->model('Systemtemplet_model');
		$templet_obj = $this->Systemtemplet_model->get_templet_obj($id);
		echo $templet_obj['content'];
	}
	
	private function post_send_data($user_id,$title,$content,$type){		
		$templetdata     = array(
				'type' =>$type,
				'user_id' =>$user_id,
				'title'   => trim($title),
				'content'   => trim($content),
				'add_time'   => time(),
		);
		$this->load->model('Systemtemplet_model');
		$sys_templet_user_send = $this->Systemtemplet_model->arrays('sys_templet_user_send');
		
		if($templetdata['title'] == ''){
			$ret['msg'] = $sys_templet_user_send[$type].'标题不能为空';
		}elseif($templetdata['content'] == ''){
			$ret['msg'] = $sys_templet_user_send[$type].'内容不能为空';	
		}
	
		if($ret['msg']){
			echo json_encode($ret);exit;
		}
		
		return $templetdata;
			
	}
	
	private function common_send($uid){
		$this->load->model('Systemtemplet_model');
		
		$ret['msginfo'] = 0;
		//发送站内信
		$msgtemplet = intval($this->input->post('msgtemplet'));		
		if($msgtemplet > 0){
			$msgdata = $this->post_send_data($uid, $this->input->post('msgtitle'), $this->input->post('msgcontent'), 3);
			$msgdata['templet'] = $msgtemplet;
			$res = $this->send_msg($msgdata);
			if($res < 1){ $ret['msginfo']="3";}
		}
		
		//发送邮件
		$emailtemplet = intval($this->input->post('emailtemplet'));
		if($emailtemplet > 0){
			$emaildata = $this->post_send_data($uid, $this->input->post('emailtitle'), $this->input->post('emailcontent'), 1);
			//$emaildata['templet'] = $emailtemplet;
			$emailres = $this->send_email($emaildata);
			if($emailres < 1){ $ret['msginfo']="1";}
		}
		
		//发送短信
		$smstemplet = intval($this->input->post('smstemplet'));
		if($smstemplet > 0){
			$smsdata = $this->post_send_data($uid, $this->input->post('smstitle'), $this->input->post('smscontent'), 2);
			//$smsdata['templet'] = $smstemplet;
			$smsres = $this->send_sms($smsdata);
			if($smsres >0){ $ret['msginfo']="2";}
		}	
		
		return $ret;
		
	}
	
	function doSingleSend(){
		$this->load->model('Systemtemplet_model');
		$sys_templet_user_send = $this->Systemtemplet_model->arrays('sys_templet_user_send');
		
		$uid = intval($this->input->post('user_id'));
		
			
		$ret = $this->common_send($uid);
		if($ret['msginfo']>0){
			$ret['msg'] = $sys_templet_user_send[$ret['msginfo']].'发送失败';
		}else{
			$ret['msg'] = 'succ';
		}
		
		echo json_encode($ret);
	   	exit;
		
	}
	
	function doSend(){
		$this->load->model('Systemtemplet_model');
		$msgtemplet = intval($this->input->post('msgtemplet'));
		$uid = $this->input->post('user_id');
		$uid_arr = explode(',', $uid);
		foreach ($uid_arr as $v){
			$ret = $this->common_send($v);	
			if($ret['msginfo']>0){
				$ret['msg'] = $sys_templet_user_send[$ret['msginfo']].'发送失败';
				echo json_encode($ret);
				exit;
			}
		}
					
		$ret['msg'] = 'succ';
		echo json_encode($ret);
   		exit;
	}
	
	
	/**
	 * 用户认证信息列表
	 */
	function attestation_all(){
		$per_page  = intval($this->input->get('per_page'));
		$page_size = 10;
		$parms     = array(
			'username'   => trim($this->input->get('username')),
			'realname'   => trim($this->input->get('realname')),
		);
		$data = array();
		$this->load->model('User_model', 'user');
		$data = $this->user->user_attestation_status($parms,$per_page);
		$data['parms'] = $parms;
		$data['kefu_list'] = $this->user->get_kefu_list();
		$this->load->library('pagination');
		$config['base_url'] = '/sys_admin/user/attestation_all?username='.$parms['username'].'&realname='.$parms['realname'];
		$config['total_rows'] = $data['count'];
		$config['per_page'] = $page_size; 
		$this->pagination->initialize($config); 
		$data['title'] = '用户认证信息列表';
		$data['p'] = $this->user->arrays();
		//var_dump($data['v']);exit;
		
		$data['tab'] = $this->tab;
		$data['sub'] = 'attestation';
		
		$this->load->view('/sys_admin/user/attestation_all', $data);
	}
	
	/**
	 * 奖励VIP期限
	 */
	function award_vip(){
		$day = $this->input->post('day');
		$user_id = $this->input->post('uid');
		//echo $day;
		$viptime = 3600*24*$day;//VIP奖励时间转成秒
		$this->load->model('User_vip_model');
		$vipinfo = $this->User_vip_model->get_by_uid($user_id);
		$time = time();
		//var_dump($vipInfo);exit;
		//echo $vipinfo['vip_status']."====".$time-$vipinfo['vip_verify_time'];exit;
		if ($vipinfo && $vipinfo['vip_status']==1 && ($time-$vipinfo['vip_verify_time']<=3600*24*365)){
			//echo 1;exit;
			//本身是有效VIP，直接延期
			$vipSetArr = array(
				'vip_verify_time' => $vipinfo['vip_verify_time']+ $viptime
			);
			$this->User_vip_model->update($vipSetArr,array('user_id'=>$user_id));
		}elseif ($vipinfo && $vipinfo['vip_status']==1 && ($time-$vipinfo['vip_verify_time']>3600*24*365)){
			//echo 2;exit;
			//已过期的  设置成奖励时间之后过期
			$vipSetArr = array(
				'vip_verify_time' => time()-(3600*24*365) + $viptime
			);
			$this->User_vip_model->update($vipSetArr,array('user_id'=>$user_id));
		}elseif (!$vipinfo){
			//echo 3;exit;
			//不是VIP的 自动成为VIP 但是奖励时间过后过期
			//$this->load->model('User_model');
			//$kefuList = $this->User_model->get_kefu_list();
			$kefu_userid = 0;//array_rand($kefuList);//随机
			$verifyTime = time()-(3600*24*365)+$viptime;
			$vipSetArr = array(
				'user_id' => $user_id,
				'kefu_userid' => $kefu_userid,
				'kefu_addtime' => $verifyTime,
				'vip_status' => 1,//自动通过无需人工审核
				'vip_remark' => '',
				'vip_money' => 0,
				'vip_verify_remark' => 'VIP审核通过',
				'vip_verify_time' => $verifyTime,
			);
			$this->User_vip_model->apply($vipSetArr);
		}
		echo "奖励成功";
	}
	
	/**
	 * 认证详情
	 */
	function attestation_info(){
		$user_id = intval($this->input->get('user_id'));
		if($user_id<1){
			$this->js->alert('数据错误');
			$this->js->go('/sys_admin/user/attestation_all');
		}
		$this->load->model('User_model', 'user');
		$data['info'] = $this->user->user_attestation_info($user_id);
		$this->load->model('Area_model', 'area');
		$data['area_list'] = $this->area->get_list();
		$this->load->model('Linkage_model', 'linkage');
		$data['minzu'] = $this->linkage->get_linkage_list(31);
		$data['hunyan'] = $this->linkage->get_linkage_list(1);
		$data['zinv'] = $this->linkage->get_linkage_list(2);
		$data['xueli'] = $this->linkage->get_linkage_list(3);
		$data['zhufang'] = $this->linkage->get_linkage_list(6);
		$data['car'] = $this->linkage->get_linkage_list(7);
		$data['gsxz'] = $this->linkage->get_linkage_list(10);
		$data['gshy'] = $this->linkage->get_linkage_list(11);
		$data['zhiwei'] = $this->linkage->get_linkage_list(13);
		$data['guanxi'] = $this->linkage->get_linkage_list(18);
		$data['title'] = '用户认证-详细信息';
		$data['user_array'] = $this->user->arrays();
		$data['tab'] = $this->tab;
		$data['sub'] = 'attestation';
		
		$this->load->view('/sys_admin/user/attestation_info', $data);
	}
	
	/**
	 * 实名认证列表
	 */
	function real_status(){
		$per_page  = intval($this->input->get('per_page'));
		$page_size = 10;
		$parms     = array(
			'username'     => trim($this->input->get('username')),
			'real_status'  => intval($this->input->get('real_status')),
		);
		$data = array();
		$this->load->model('User_model', 'user');
		$data = $this->user->real_status_list($parms,$per_page);
		$this->load->model('Area_model', 'area');
		$data['area_list'] = $this->area->get_list();
		$this->load->model('Linkage_model', 'linkage');
		$data['minzu'] = $this->linkage->get_linkage_list(31);
		$data['parms'] = $parms;
		
		$this->load->library('pagination');
		$config['base_url'] = '/sys_admin/user/real_status?real_status='.$parms['real_status'].'&username='.$parms['username'];
		$config['total_rows'] = $data['count'];
		$config['per_page'] = $page_size; 
		$this->pagination->initialize($config); 
		$data['title'] = '用户认证-实名认证';
		$data['user_array'] = $this->user->arrays();
		
		$data['tab'] = $this->tab;
		$data['sub'] = 'real';
		
		$this->load->view('/sys_admin/user/attestation_realname', $data);
	}
	
	/**
	 * 实名认证
	 */
	function attestation_realname(){
		$parms     = array(
			'user_id'      => intval($this->input->post('user_id')),
			'real_status'  => intval($this->input->post('real_status')),
			'remark'       => trim($this->input->post('remark')),
		);
		
		if($parms['user_id'] < 1 || !in_array($parms['real_status'],array(0,1,2))){
			echo "数据错误";
			exit;
		}
		//获取操作人
		$adminUser = $this->session->userdata('admin_user');
		if(!isset($adminUser['user_id']) || $adminUser['user_id'] < 1){
			echo "错误";
			exit;
		}
		$this->load->model('User_model', 'user');
		$result = $this->user->update(array('real_status'=>$parms['real_status']),array('user_id'=>$parms['user_id']));
		if($result && $parms['real_status']==1){
			
			$this->notice_credit($parms['user_id'], 'real');
			
			$userinfo = $this->user->get_one(array('user_id'=>$parms['user_id']));
			if($userinfo['card_pic1'] && $userinfo['card_pic2']){
				$this->notice_credit($parms['user_id'], 'idcard');				
			}
			
		}
		//发送站内信提醒
		$this->notice_message($parms['user_id'], '实名认证', $parms['remark']);
		
		
		echo $result ? '操作成功' : '操作失败';
	}

	function edit($user_id=''){
		$data = array();
		$user_id = intval($user_id);
		$this->load->model('User_model', 'user');
		$info = $this->user->get_by_uid($user_id);
		$act = $this->input->post('act');
		if($act == 'edit'){
			$setArr = array(
				'email' => $this->input->post('email'),
				'realname' => $this->input->post('realname'),
				'sex' => $this->input->post('sex'),
				'birthday' => $this->input->post('birthday') ? strtotime($this->input->post('birthday')) : 0,
				'is_loan' => $this->input->post('is_loan'),
				'fstatus' => $this->input->post('fstatus'),
				'is_self' => $this->input->post('is_self'),
				'card_type' => $this->input->post('card_type'),
				'card_id' => $this->input->post('card_id'),
				'phone' => $this->input->post('phone'),
				'phone_status' => $this->input->post('phone_status'),
				'real_status' => $this->input->post('real_status'),
				//'invite_userid' => $this->input->post('invite_userid'),
				'error_num'=>$this->input->post('error_num'),
				'type_id'=>$this->input->post('type_id'),
				'islock'=>$this->input->post('islock'),
			);
			
			if ($setArr['fstatus']==3){
				$adminUser = $this->session->userdata('admin_user');
				if($adminUser['type_id']!=1){
					$ret = array('msg'=>'权限不足','url'=>'');
					echo json_encode($ret);
					exit; 
				}
			}

			/****** by liub 20141214 屏蔽邮箱验证
			if(!$setArr['email']){
				$ret = array('msg'=>'邮箱不能为空','url'=>'');
				echo json_encode($ret);
				exit; 
			}*/
			
			$pwd = $this->input->post('password');
			$pwd2 = $this->input->post('password2');
			if($pwd && $pwd==$pwd2){
				$setArr['password'] = md5(md5($pwd));
			}else{
				if($pwd && $pwd!=$pwd2){
					$ret = array('msg'=>'两次密码输入不一致','url'=>'');
					echo json_encode($ret);
					exit; 
				}
			}
			
			$this->user->update($setArr,array('user_id'=>$user_id));
			$ret = array('msg'=>'修改成功','url'=>'');
			$this->load->model('log_model');$this->log_model->add_log('[修改][用户]ID=='.$user_id);
			echo json_encode($ret);
			exit; 
		}
		
		
		$data['varr'] = $this->user->arrays();
		$data['info'] = $info;
		$data['title'] = '用户信息-基础信息';
		
		$data['tab'] = $this->tab;
		$data['sub'] = 'ulist';
		
		$this->load->view('/sys_admin/user/user_edit', $data);
	}
	
	function add(){
		$data = array();
		$this->load->model('User_model', 'user');
		$act = $this->input->post('act');
		if($act == 'add'){
			$setArr = array(
				'username' => $this->input->post('username'),
				'email' => $this->input->post('email'),
				'realname' => $this->input->post('realname'),
				'sex' => $this->input->post('sex'),
				'birthday' => $this->input->post('birthday') ? strtotime($this->input->post('birthday')) : 0,
				'type_id' => $this->input->post('type_id'),
				'fstatus' => $this->input->post('fstatus'),
				'card_type' => $this->input->post('card_type'),
				'card_id' => $this->input->post('card_id'),
				'phone' => $this->input->post('phone'),
				'qq' => $this->input->post('qq')
			);
			
			$adminUser = $this->session->userdata('admin_user');
			if ($setArr['type_id']==1 && $adminUser['user_id']!=1){
				$ret = array('msg'=>'权限不足','url'=>'');
				echo json_encode($ret);
				exit; 
			}
			if ($setArr['type_id']!=3 && $adminUser['type_id']!=1){
				$ret = array('msg'=>'权限不足','url'=>'');
				echo json_encode($ret);
				exit; 
			}
			
			
			if ($setArr['username']==""){
				$ret['msg'] = '用户名不能为空';
				echo json_encode($ret);exit;
			}
			
			if(!$setArr['email']){
				$ret = array('msg'=>'邮箱不能为空','url'=>'');
				echo json_encode($ret);
				exit; 
			}
			
			if($this->user->get_one(array('username'=>$setArr['username']), 'user_id')){
				$ret['msg'] = '用户名已存在';
				echo json_encode($ret);exit;
			}elseif($this->user->get_one(array('email'=>$setArr['email']), 'user_id')){
				$ret['msg'] = '邮箱已存在';
				echo json_encode($ret);exit;
			}
			
			$pwd = $this->input->post('password');
			$pwd2 = $this->input->post('password2');
			if($pwd && $pwd==$pwd2){
				$setArr['password'] = md5(md5($pwd));
			}else{
				if($pwd && $pwd!=$pwd2){
					$ret = array('msg'=>'两次密码输入不一致','url'=>'');
					echo json_encode($ret);
					exit; 
				}
			}
			$setArr['add_time'] =time();
			$this->user->add($setArr);
			$ret = array('msg'=>'添加成功','url'=>'');
			$username =$this->input->post('username');
			$this->load->model('log_model');$this->log_model->add_log('[添加][用户]['.$username.']');
			echo json_encode($ret);
			exit; 
		}
		$data['v'] = $this->user->arrays();
		$data['title'] = '用户信息-基础信息';
		
		$data['tab'] = $this->tab;
		$data['sub'] = 'user_add';
		
		$this->load->view('/sys_admin/user/user_add', $data);
	}
	
	function attlist($user_id=''){
		$data = array();
		$user_id = intval($user_id);
		$this->load->model('User_model', 'user');
		$data['userinfo'] = $this->user->get_by_uid($user_id);
		$this->load->model('Attestation_model', 'attestation');
		$this->load->model('Attestation_type_model', 'attestation_type');
		$data['type_list'] = $this->attestation_type->get_id_names();
		$data['status_list'] =  $this->attestation->fstatus();
		$list = $this->attestation->get_list(array('user_id'=>$user_id));
		$data['list'] = $list ? $list : array();
		
		$data['tab'] = $this->tab;
		$data['sub'] = 'ulist';
		
		$this->load->view('/sys_admin/user/user_attlist',$data);
	}
	
	function update_att($id){
		$id = intval($id);
		$this->load->model('Attestation_model', 'attestation');
		$this->load->model('Attestation_type_model', 'attestation_type');
		$this->load->model('User_model', 'user');
		$att_info = $this->attestation->get_one(array('id'=>$id));
		
		$user = $this->user->get_by_uid($att_info['user_id']);
		$att_info['username'] = $user['username'];
		
		if(isset($_POST['act']) && $_POST['act'] == 'edit'){
			if($att_info){
				$parms = array( 'type_id' => $this->input->post('type_id'),
								'name'    => $this->input->post('name'));
				
				$user_id = $this->input->post('user_id');
				$edit_id = $this->input->post('id');
				
				if(isset($_FILES['litpic']) && $_FILES['litpic']['name'] != ''){
					$ret_litpic = $this->upload_litpic_data($user_id);
					if($ret_litpic['msg'] == 'ok'){
						$parms['litpic'] = $ret_litpic['litpic'];
					}else{
						$this->js->alert($ret_litpic['msg']);exit;
					}
				}
					
				$where = array('id'=>$edit_id,'user_id'=>$user_id);
				$res = $this->attestation->update($parms,$where);
				if($res){
					$ret['msg'] = '保存成功';
					$ret['url'] = '/sys_admin/user/update_att/'.$edit_id;
					$this->js->alert($ret['msg']);
					$this->js->go($ret['url']);
					exit;
					
				}else{
					$ret['msg'] = '保存失败';
					$this->js->alert($ret['msg']);exit;
				}
					
				
			}
		}else{
			$data['type_list'] = $this->attestation_type->get_id_names();
			$data['att_info'] =$att_info;
			$data['tab'] = $this->tab;
			$data['sub'] = 'documentation';
			$this->load->view('/sys_admin/user/user_attedit',$data);
		}
		
	}
	
	
	function del_att($id=''){
		$id = intval($id);
		$this->load->model('Attestation_model', 'attestation');
		$res = $this->attestation->delete(array('id'=>$id));
		$act = $this->input->post('act');
		$username = $this->input->post('username');
		if($res){
			$ret = array('msg'=>'删除成功','url'=>'');
			$this->load->model('log_model');$this->log_model->add_log('用户'.$username.'删除成功');
			echo json_encode($ret);
			exit; 
		}else{
			$ret = array('msg'=>'删除失败','url'=>'');
			$this->load->model('log_model');$this->log_model->add_log('用户'.$username.'删除失败');
			echo json_encode($ret);
			exit; 
		}
		
	}
	
	function check_att(){
		$this->load->model('Attestation_model', 'attestation');
		$this->load->model('Credit_model', 'credit');
		
		$id = $this->input->post("id");
		$fstatus = $this->input->post('fstatus');
		$jifen = 0;
					
		if($fstatus == 1){
			$credit_info = $this->credit->get_credit_rule(array('nid'=>'zhengjian'));
			$jifen = $credit_info['credit_value'];
		}
		
		$setArr = array(
				'fstatus'  => $fstatus,
				'jifen'   => $jifen,
				'verify_time' => time(),
				'verify_user' => 1,
				'verify_remark' => $this->input->post('verify_remark'),
			);
		
		if($id>0){
			$attinfo = $this->attestation->get_one(array('id'=>$id));
			$where = array('user_id'=>$attinfo['user_id'],'id'=>$id);
			$res = $this->attestation->update($setArr,$where);
			if($res>0){
				//积分增加
				if($jifen>0){
					$log = array(
						'user_id' => $user['user_id'],
						'type_id' => $type_id,
						'op' => 1,
						'credit_value' => $jifen,
						'remark' => $credit_info['name'].'积分加'.$jifen.'分',
						'op_user' => 1,
						'add_time' => time(),
						'addip' => '',
					);
					$this->credit->incr_credit($log);
				}
				echo  '操作成功';
				exit;
			}else{
				echo  '操作失败';
				exit;
			}
		}
		
	}
	
	function getinfo(){
		$user_id = intval($this->input->post('user_id'));
		$ret['msg'] = '数据错误';
		if($user_id>0){
			$this->load->model('User_model');
			$user = $this->User_model->get_by_uid($user_id);
			if($user){
				$tmp = array(0=>'-',1=>'男',2=>'女');
				$user['sex'] = $tmp[$user['sex']];
				
				if($user['nation']){
					$this->load->model('Linkage_model', 'linkage');
					$tmp = $this->linkage->get_linkage_list(31);
					$user['nation'] = $tmp[$user['nation']];
				}else{
					$user['nation'] = '-';
				}
				
				$user['birthday'] = $user['birthday'] ? date('Y-m-d',$user['birthday']) : '-';
				
				$jiguan = '';
				$this->load->model('Area_model', 'area');
				$areaList = $this->area->get_list();
				$user['province'] && $jiguan .= $areaList[$user['province']];
				$user['city'] && $jiguan .= $areaList[$user['city']];
				$user['area'] && $jiguan .= $areaList[$user['area']];
				$user['jiguan'] = $jiguan ? $jiguan : '-';
				
				
				$ret['msg'] = '';
				$ret['user'] = $user;
			
				$this->load->model('User_vip_model');
				$uservip = $this->User_vip_model->get_by_uid($user_id);
				if($uservip){
					$kefuList = $this->User_model->get_kefu_list();
					$user['kefu'] = $kefuList[$uservip['kefu_userid']]['username'];
				}
				$ret['user'] = $user;
			}
			
		}
		echo json_encode($ret);
	}
	
	function leve(){
		
		$this->load->model('Credit_model');
		
		$data['tab'] = $this->tab;
		$data['sub'] = 'user_rank';
		
		$data['list'] = $this->Credit_model->get_credit_rank();
		$this->load->view('/sys_admin/credit/credit_rank',$data);
	}
	
	function leve_synchronize(){
		$this->load->model('Credit_model');
		$id = $this->input->post("id");
		$info = $this->Credit_model->get_credit_rank_one($id);
		$ret = array('msg'=>$info);
		echo json_encode($ret);
		exit; 
	}
	
	// 原积分变更，新增 保存
	function credit_rank_save(){
		$this->load->model('Credit_model');
		$admin = $this->session->userdata('admin_user');
		if(isset($admin['type_id']) && $admin['type_id'] ==1){
			$act = $this->input->post("act");
			$parms = array(	'creidt_rank'=>$this->input->post("creidt_rank"),
							'point1'=>$this->input->post("point1"),
							'point2'=>$this->input->post("point2"),
							'pic'=>$this->input->post("pic"),
						);
								
			if($act == 'edit'){
				$id = $this->input->post("id");
				$res = $this->Credit_model->update_credit_rank($id,$parms);
			}else if($act == 'delete'){
				$id = $this->input->post("id");
				$res = $this->Credit_model->del_credit_rank($id);
			}else{
				$res = $this->Credit_model->add_credit_rank($parms);
			}
			if($res>0){
				$ret = array('msg'=>'操作成功');
			}else{
				$ret = array('msg'=>'操作失败');
			}
			echo json_encode($ret);
			exit; 
		}else{
			$ret = array('msg'=>'对不起，权限不足');
			echo json_encode($ret);
			exit;
		}
		
		
	}
	
	function documentation(){
		$this->load->model('Attestation_model', 'attestation');
		$this->load->model('Attestation_type_model', 'attestation_type');
		
		$this->load->model('User_model', 'user');
		
		$username = $this->input->get("username");
		
		
		$parms = array('is_del'=>0);
		
		if($username){
			$parms['username']=$username;
		}
		
		$fstatus = $this->input->get("fstatus");
		if($fstatus !=-1){
			$parms['fstatus'] = $fstatus;
		}
		
		
		$per_page  = intval($this->input->get('per_page'));
		$page_size = 10;
		
		$vdata = $this->attestation->get_attestation_list($parms,$per_page,$page_size);
		
		$vdata['kefu_list'] = $this->user->get_kefu_list();
		$vdata['type_list'] = $this->attestation_type->get_id_names();
		$vdata['status_list'] =  $this->attestation->fstatus();
		//分页
		$this->load->library('pagination');
		$config['base_url'] = '/sys_admin/user/documentation?username='.$parms['username'];
		$config['total_rows'] = $vdata['count'];
		$config['per_page'] = $page_size; 
		$this->pagination->initialize($config); 
		
		$vdata['tab'] = $this->tab;
		$vdata['sub'] = 'documentation';	
		$vdata['parms'] = $parms;
		$this->load->view('/sys_admin/user/user_document', $vdata);
		
	}
	
	function data_upload(){
		$this->load->model('Attestation_model', 'attestation');
		$this->load->model('Attestation_type_model', 'attestation_type');
		
		$vdata['type_list'] = $this->attestation_type->get_id_names();
		$vdata['tab'] = $this->tab;
		$vdata['sub'] = 'documentation';
		$this->load->view('/sys_admin/user/user_dataupload', $vdata);
	}
	
	function doDataUpload(){
		$ret = array('msg' => '','url' => '');
			
			$username = $this->input->post("username");
			if(!$username){
				$ret['msg'] = '用户名不能为空';
				$this->js->alert($ret['msg']);exit;
			}
			$this->load->model('Credit_model', 'credit');
			$credit_info = $this->credit->get_credit_rule(array('nid'=>'zhengjian'));
			$type_id=$credit_info['id'];
			
			/*$jifen = $credit_info['credit_value'];*/
			$jifen = 0;
			
			$this->load->model('User_model', 'user');
			$userinfo = $this->user->get_one(array('username'=>$username));
			if(!$userinfo){
				$ret['msg'] = '此用户名不存在';
				$this->js->alert($ret['msg']);exit;
			}
			
			
			$setArr = array(
				'user_id' => $userinfo['user_id'],
				'type_id' => $this->input->post('type_id'),
				'name'    => $this->input->post('name'),
				'fstatus'  => 0,
				'jifen'   => $jifen,
				'content' => $this->input->post('name'),
				'verify_time' => 0,
				'verify_user' => 1,
				'verify_remark' => '待审核',
				'add_time' => time(),
				'addip' => $this->input->ip_address(),
			);
			
			$this->load->model('Attestation_model', 'attestation');
			$ret_litpic = $this->upload_litpic_data($userinfo['user_id']);
			if($ret_litpic['msg'] == 'ok'){
				$setArr['litpic'] = $ret_litpic['litpic'];
				$res = $this->attestation->add($setArr);
				if($res){
					$ret['msg'] = '上传成功';
					$this->js->alert($ret['msg']);exit;
					
				}else{
					$ret['msg'] = '保存失败';
					$this->js->alert($ret['msg']);exit;
				}
			}else{
				$this->js->alert($ret_litpic['msg']);exit;
			}			
	}
	
	private function upload_litpic_data($user_id){
			$path = 'static/data/upfiles/images/'.$user_id.'/';
			if(!file_exists($path)){
				mkdir($path,0777);
			}
			$config['upload_path'] = $path;
			$config['allowed_types'] = 'jpg|png';
			$config['file_name'] = 'attestation_'.uniqid();
			$config['overwrite'] = true;
			$config['max_size'] = '512';
			$config['max_width']  = '2048';
			$config['max_height']  = '3000';
			
			$this->load->library('upload', $config);
			if($this->upload->do_upload('litpic')){
				$tmp = $this->upload->data();
				$litpic = 'static/data/upfiles/images/'.$user_id.'/'.$config['file_name'].$tmp['file_ext'];
				
				//添加水印
				$this->load->library('image_lib');
				$warter['source_image'] = $litpic;
				$warter['wm_type'] = 'overlay';
				$warter['wm_overlay_path'] = 'static/images/logo.png';
				$warter['wm_opacity'] = '60';
				$warter['wm_vrt_alignment'] = 'middle';
				$warter['wm_hor_alignment'] = 'center';
				$this->image_lib->initialize($warter);
				$this->image_lib->watermark();			
				$msg = 'ok';
			}else{
				$litpic = '';
				$msg = $this->upload->display_errors();
			}
			
			return array('litpic'=>$litpic,'msg'=>$msg);
	}
	
	//vip列表
	function vip_ulist(){
		$per_page  = intval($this->input->get('per_page'));
		$page_size = 10;
		$parms     = array(
			'username'     => trim($this->input->get('username')),
			'vip_status'  => intval($this->input->get('vip_status')),
		);
		$data = array();
		$this->load->model('User_model', 'user');
		$data = $this->user->vip_status_list($parms,$per_page);
		$data['parms'] = $parms;
		$data['kefu_list'] = $this->user_model->get_kefu_list();
		$this->load->library('pagination');
		$config['base_url'] = '/sys_admin/user/vip_ulist?username='.$parms['username'].'&vip_status='.$parms['vip_status'];
		$config['total_rows'] = $data['count'];
		$config['per_page'] = $page_size; 
		$this->pagination->initialize($config); 
		$data['title'] = '用户管理-VIP审核';
		$data['statusArr'] = $this->user->arrays('statusArr');
		$data['tab'] = $this->tab;
		$data['sub'] = 'user_vip';
		$this->load->view('/sys_admin/user/attestation_vip', $data);
	}
	
	function vip_status(){		
		$userid 			= $this->input->post("user_id");
		$vip_status 		= $this->input->post("vip_status");
		$vip_verify_remark	= $this->input->post("vip_verify_remark");
		
		$this->load->model('User_vip_model', 'vip');
		$setArr = array(
				'vip_status' => $vip_status,
				'vip_verify_remark' => $vip_verify_remark,
				'vip_verify_time' => time(),
			);	
		$where = array('user_id'=>$userid);	
		$res = $this->vip->update($setArr,$where);
		if($res){
			
			$this->notice_credit($user_id, 'vip');
						
			$name = $vip_status?'VIP审核通过':'VIP审核未通过';
			$content = $vip_status?'您的vip申请已审核通过，祝你投资愉快！':'您的vip申请未审核通过!';
			$this->notice_message($userid, $name, $content);
			
			
			echo '操作成功';
			exit; 
		}else{
			echo '操作失败';
			exit; 
		}
		
	}
	
	public function datum(){
		$this->load->model('attestation_type_model');
		
		$per_page  = intval($this->input->get('per_page'));
		$page_size = 10;
		
		$data = $this->attestation_type_model->get_datum_list(array('fstatus'=>1),$per_page,$page_size);
		
		$this->load->library('pagination');
		$config['base_url'] = '/sys_admin/user/datum?';
		$config['total_rows'] = $data['count'];
		$config['per_page'] = $page_size; 
		$this->pagination->initialize($config); 
		
		$data['tab'] = $this->tab;
		$data['sub'] = 'user_datum';
		
		$this->load->view('/sys_admin/user/user_datum',$data);
	}
	
	public function user_datum_save(){
		$this->load->model('attestation_type_model');
		
		$parms = array(	'name'=>$this->input->post("name"));
		$id = intval($_POST['id']);		
		$act = $_POST['act'];
		if(isset($act) && $act == 'edit'){
			$res = $this->attestation_type_model->update($parms,array('type_id'=>$id));
		}elseif($act == 'del'){
			$res = $this->attestation_type_model->update(array('fstatus'=>2),array('type_id'=>$id));
		}elseif($act == 'add'){
			$parms['fstatus'] = 1;
			$res = $this->attestation_type_model->add($parms);
		}			
		
		if($res>0){
			$ret = array('msg'=>'操作成功');
		}else{
			$ret = array('msg'=>'操作失败');
		}
		echo json_encode($ret);
		exit; 
	}
	
	
	function showDetailInfo(){
		$user_id = intval($this->input->post('user_id'));
		
		$this->load->model('User_model');
		$this->load->model('Borrow_model');
		$this->load->model('User_vip_model');
		$this->load->model('Credit_model');
		$this->load->model('Area_model', 'area');
		$this->load->model('Account_model');
		$this->load->model('Accountcash_model');
		
		$data['info'] = $this->User_model->user_attestation_info($user_id);
		$data['sex'] = $this->User_model->arrays('sex');
		$data['statusArr'] = $this->User_model->arrays('statusArr');
		$data['cardType'] = $this->User_model->arrays('cardType');
		
		$data['u_credit'] = $this->Credit_model->get_row($user_id);
		$data['u_credit']['credit_value'] = isset($data['u_credit']['credit_value']) ? $data['u_credit']['credit_value'] : 0;
		$data['u_credit']['pic'] = $this->Credit_model->get_pic($data['u_credit']['credit_value']);
				
		$data['tender_list'] = $this->Borrow_model->get_borrow_tender_list(array('select'=>1,'page_size'=>5,'page_start'=>0,'where'=>array('user_id'=>$user_id,'fstatus'=>1)));
		
		$cdata['where']['username']=$data['info']['username'];
		$cdata['page_start'] = 0;
		$cdata['page_size'] = 1;
		$account = $this->Account_model->get_account_user_list($cdata);
		$data['account_list'] = $account['account_list'];
		$data['area_list'] = $this->area->get_list();
		
		$data['name_list'] = $this->User_model->get_name_list();
		
		$data['account_bank'] = $this->Accountcash_model->arrays("account_bank");
		
		$response_html = $this->load->view('/sys_admin/user/show_user_info',$data,true);
		
		//print_r($response_html);eixt;
		
		echo $response_html;
	}
	
	
	function showUsername(){
		
		$this->load->model('User_model');
		$data['user_list'] = $this->User_model->get_name_list();
		$response_html = $this->load->view('/sys_admin/user/show_user_name',$data,true);
		
		echo $response_html;
	}
	
	
	
	//****** by liub 20141113 锁定会员列表
	
	//TODO 可优化，可在页面传固定值过来进行登陆判别,0=会员列表，1=黑名单会员列表，2=锁定会员列表
	function zhs_lock($islock){
		$per_page  = intval($this->input->get('per_page'));
		$page_size = 10;
		$parms     = array(
			'username'   => trim($this->input->get('username')),
			'realname'   => trim($this->input->get('realname')),
			'email'      => trim($this->input->get('email')),
			'type_id'	 => intval($this->input->get('type_id')),
			'add_time'	 => $this->input->get('add_time'),
			
		);
		
		$data = array();
		
		$this->load->model('User_model', 'user');
		
		$islock = 2;
		$data = $this->user->get_locku_list($parms,$per_page,$islock );
		$data['parms'] = $parms;
		
		//分页
		/*$this->load->library('pagination');
		$config['base_url'] = '/sys_admin/user/ulist?username='.$parms['username'].'&realname='.$parms['realname'].'&email='.$parms['email'].'&type_id='.$parms['type_id'].'&add_time='.$parms['add_time'];
		$config['total_rows'] = $data['user_count'];
		$config['per_page'] = $page_size; 
		$this->pagination->initialize($config); 
		$data['title'] = '用户列表';*/
		
		
		$data['userFstatus'] = $this->user->arrays('userFstatus');
		
		$data['tab'] = $this->tab;
		$data['sub'] = 'lockulist';
		
		$this->load->view('/sys_admin/user/lockuser_list', $data);
	
	}
	//****** by liub 20141113  黑名单会员列表
	function zhs_blacklist(){
		$per_page  = intval($this->input->get('per_page'));
		$page_size = 10;
		$parms     = array(
			'username'   => trim($this->input->get('username')),
			'realname'   => trim($this->input->get('realname')),
			'email'      => trim($this->input->get('email')),
			'type_id'	 => intval($this->input->get('type_id')),
			'add_time'	 => $this->input->get('add_time'),
			'real_status'	 => 2,
		);
		
		$data = array();
		
		$this->load->model('User_model', 'user');
		
		$islock = 1;
		$data = $this->user->get_locku_list($parms,$per_page,$islock,$page_size);
		$data['parms'] = $parms;
		
		//分页
		$this->load->library('pagination');
		$config['base_url'] = '/sys_admin/user/zhs_blacklist?username='.$parms['username'].'&realname='.$parms['realname'].'&email='.$parms['email'].'&type_id='.$parms['type_id'].'&add_time='.$parms['add_time'].'&real_status=2';
		$config['total_rows'] = $data['user_count'];
		$config['per_page'] = $page_size; 
		$this->pagination->initialize($config); 
		$data['title'] = '用户列表';
		
		
		$data['userFstatus'] = $this->user->arrays('userFstatus');
		$data['tab'] = $this->tab;
		$data['sub'] = 'blacklist';
		
		$this->load->view('/sys_admin/user/blackuser_list', $data);
	
	}
	
	//****** by liub 20141113  解除黑名单和锁定
	function remove(){
		$user_id = intval($this->input->post('user_id'));
		$setArr = array('islock' => 0);	
		$this->load->model('User_model', 'user');
  		echo $this->user->update($setArr,array('user_id'=>$user_id));
	}
	
	//****** by liub 20141117 投资积分列表
	function zhs_uviplevel(){
		$this->load->model('Invest_model');
		
		$data['list'] = $this->Invest_model->get_invest_rank_list();
		$data['tab'] = $this->tab;
		$data['sub'] = 'userviplevel';
		
		$this->load->view('/sys_admin/user/uviplevel_list', $data);
	}
	
	//****** by liub 20141117 投资积分保存
	function uviplsave(){
		$this->load->model('Invest_model');
		
		$admin = $this->session->userdata('admin_user');
		if(isset($admin['type_id']) && $admin['type_id'] ==1){
			$act = $this->input->post("act");
			
			//从前台获取传入参数
			$parms = array(	'vip_name'=>$this->input->post("vip_name"),
							'minscore'=>$this->input->post("minscore"),
							'maxscore'=>$this->input->post("maxscore"),
							'investpercent'=>$this->input->post("investpercent"),
							'pic'=>$this->input->post("pic"),
							'add_time' => time (), 
							'addip' => $this->input->ip_address (),
							'op_user' => $admin['id'], 
						);
			// TODO 增加日志				
			if($act == 'add'){
				$res = $this->Invest_model->add_invest_rank($parms);
			}else if($act == 'delete'){
				$id = $this->input->post("id");
				$res = $this->Invest_model->del_invest_rank($id);
			}elseif($act == 'edit'){
				$id = $this->input->post("id");
				$res = $this->Invest_model->update_invest_rank($id,$parms);
			}
			if($res>0){
				$ret = array('msg'=>'操作成功');
			}else{
				$ret = array('msg'=>'操作失败');
			}
		}else{
			$ret = array('msg'=>'对不起，权限不足！');
		}
		
		echo json_encode($ret);
		
		exit; 
	}
	
	//excel到出会员列表
	function userListAllExcel(){
		if($_GET){
			echo '<script>alert("不能下载Excel");</script>';
			exit;
		}
		$data=array();
		$this->load->model('User_model');
		$data = $this->User_model->get_user_list('',0,500);	
		//var_dump($data['user_list']);exit;
		$this->load->library('PHPExcel');
		$this->load->library('PHPExcel/IOFactory'); 
		$objPHPExcel = new PHPExcel();
		$objPHPExcel->getProperties()->setTitle("投资人列表")->setDescription(COMPANY_NAME);
		$objPHPExcel->setActiveSheetIndex(0);
		$titles = array("序号", "会员名", "累计充值金额", "累计投标数量", "累计投标金额", "账户总额", "邮箱", "手机", "介绍人", "注册时间", "最后登录时间"); 
		for($i = 0; $i < count($titles); $i ++){
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($i, 1, $titles[$i]);
		}
		$accountlog_list = $data['user_list'];
		for($i = 0; $i < count($accountlog_list); $i++){
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(0, $i+2, $accountlog_list[$i]['user_id']);
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(1, $i+2, $accountlog_list[$i]['username']);
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(2, $i+2, isset($accountlog_list[$i]['recharge_money'])?$accountlog_list[$i]['recharge_money']:0);
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(3, $i+2, $accountlog_list[$i]['tender_count']);
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(4, $i+2, isset($accountlog_list[$i]['tender_money'])?$accountlog_list[$i]['tender_money']:0);
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(5, $i+2, $accountlog_list[$i]['use_money']);
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(6, $i+2, $accountlog_list[$i]['email']);
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(7, $i+2, $accountlog_list[$i]['phone']);
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(8, $i+2, $accountlog_list[$i]['invite_uname']);
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(9, $i+2, date('Y-m-d H:i:s', $accountlog_list[$i]['add_time']));
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(10, $i+2, date('Y-m-d H:i:s', $accountlog_list[$i]['lasttime']));
			$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(11, $i+2, $accountlog_list[$i]['remark']);
		}
		
		$objWriter = IOFactory::createWriter($objPHPExcel, 'Excel5'); 
		//发送标题强制用户下载文件 
		header('Content-Type: application/vnd.ms-excel'); 
		header('Content-Disposition: attachment;filename="投资人列表.xls"'); 
		header('Cache-Control: max-age=0'); 
		$objWriter->save('php://output'); 
	}

}
